package pl.kosmowska.ewelina.client.views.impl;


import pl.kosmowska.ewelina.client.app.ClientFactory;
import pl.kosmowska.ewelina.client.app.activity.places.BankStatementImportPlace;
import pl.kosmowska.ewelina.client.app.activity.places.CategoryPlace;
import pl.kosmowska.ewelina.client.app.activity.places.ChartPlace;
import pl.kosmowska.ewelina.client.app.activity.places.IncomeOutcomePlace;
import pl.kosmowska.ewelina.client.app.activity.places.IncomePlace;
import pl.kosmowska.ewelina.client.app.activity.places.InvestmentPlace;
import pl.kosmowska.ewelina.client.app.activity.places.LoanPlace;
import pl.kosmowska.ewelina.client.app.activity.places.OutcomePlace;
import pl.kosmowska.ewelina.client.app.activity.places.SavingsPlace;
import pl.kosmowska.ewelina.client.app.activity.places.StockPlace;
import pl.kosmowska.ewelina.client.app.user.AuthEvent;
import pl.kosmowska.ewelina.client.app.user.AuthEvent.Auth;
import pl.kosmowska.ewelina.client.app.user.AuthEventHandler;
import pl.kosmowska.ewelina.client.views.images.UiImages;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;

public class LeftSideViewImpl extends Composite {

	private static LeftSideUiBinder uiBinder = GWT
			.create(LeftSideUiBinder.class);

	interface LeftSideUiBinder extends UiBinder<Widget, LeftSideViewImpl> {
	}


	@UiField SimplePanel menuLayout;

	ClientFactory clientFactory;

	private boolean alreadyInit;

	public LeftSideViewImpl(ClientFactory clientFactory) {
		initWidget(uiBinder.createAndBindUi(this));

		setClientFactory(clientFactory);
		startView();
		initAuthorisationForView();


	}

	private void startView() {
		if (clientFactory.getIsLoggedIn() == null) {
			// wait for login event b/c hasn't happened yet
		} else if (clientFactory.getIsLoggedIn() == true) {
			setLoggedIn();
		} else if (clientFactory.getIsLoggedIn() == false) { 
			setLoggedOut();
		}
	}

	private void initAuthorisationForView() {
		if (alreadyInit == false) {
			clientFactory.getEventBus().addHandler(AuthEvent.TYPE, new AuthEventHandler() {
				public void onAuthEvent(AuthEvent event) {
					Auth e = event.getAuthEvent();
					if (e == Auth.LOGGEDIN) {
						setLoggedIn();
					} else if (e == Auth.LOGGEDOUT) {
						setLoggedOut();
					}
				}
			});
		}
		alreadyInit = true;
	}

	private void setLoggedOut() {
		menuLayout.clear();	
	}

	private void setLoggedIn() {
		menuLayout.add(createImage());		
	}

	public void setClientFactory(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;

	}


	public StackLayoutPanel createImage() {
		// Get the images.
		UiImages uiImages = GWT.create(UiImages.class);

		// Create a new stack layout panel.
		StackLayoutPanel stackPanel = new StackLayoutPanel(Unit.EM);
		stackPanel.setPixelSize(200, 600);

		// Add the Mail folders.
		Widget currentHeader = createHeaderWidget(
				"&nbsp;&nbsp;Bieżace transakcje", uiImages.currentBudget());
		stackPanel.add(createCurrentBudgetItem(uiImages), currentHeader, 4);

		// Add a list of charts.
		Widget chartsHeader = createHeaderWidget(
				"&nbsp;&nbsp;Zestwienia",  uiImages.charts());
		stackPanel.add(createChartsItem(uiImages), chartsHeader, 4);

		// Add a list of loans.
		Widget loansHeader = createHeaderWidget(
				"&nbsp;&nbsp;Kredyty",  uiImages.loans());
		stackPanel.add(createLoanItem(uiImages), loansHeader, 4);

		// Add a list of investments.
		Widget investmentsHeader = createHeaderWidget(
				"&nbsp;&nbsp;Inwestycyje",  uiImages.investments());
		stackPanel.add(createInvestmentsItem(uiImages), investmentsHeader, 4);


		// Add a list of category.
		Widget categoryHeader = createHeaderWidget(
				"&nbsp;&nbsp;Kategorie",  uiImages.categories());
		stackPanel.add(createCategoryItem(uiImages), categoryHeader, 4);

		// Add a list of planer.
		Widget planerHeader = createHeaderWidget(
				"&nbsp;&nbsp;Planer",  uiImages.planer());
		stackPanel.add(createPlanerItem(uiImages), planerHeader, 4);

		// Add a list of import.
		Widget importHeader = createHeaderWidget(
				"&nbsp;&nbsp;Import wyciagów",  uiImages.importOutcomes());
		stackPanel.add(createImportItem(uiImages), importHeader, 4);

//		// Add a list of info.
//		Widget infoHeader = createHeaderWidget(
//				"&nbsp;&nbsp;Informacje",  uiImages.help());
//		stackPanel.add(createInfoItem(uiImages), infoHeader, 4);

		stackPanel.ensureDebugId("cwStackLayoutPanel");
		return stackPanel;
	}

	private Widget createHeaderWidget(String text, ImageResource image) {
		// Add the image and text to a horizontal panel
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setHeight("100%");
		hPanel.setSpacing(0);
		hPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		hPanel.add(new Image(image));
		HTML headerText = new HTML(text);
		headerText.setStyleName("cw-StackPanelHeader");
		hPanel.add(headerText);
		return new SimplePanel(hPanel);
	}
	private Widget createCurrentBudgetItem(UiImages uiImages) {
		Tree mailPanel = new Tree();
		TreeItem mailPanelRoot = mailPanel.addItem(this.clientFactory.getUserData().getGoogleNickname());
		addItem(mailPanelRoot, uiImages.incomesAndOutcomes()," Wszystko");
		addItem(mailPanelRoot, uiImages.outcomes(), " Wydatki" );
		addItem(mailPanelRoot, uiImages.incomes(), " Przychody" );
		mailPanel.addSelectionHandler(new SelectionHandler()
		{
			@Override
			public void onSelection(SelectionEvent event)
			{
				switchView(((TreeItem) event.getSelectedItem()).getText());
			}

			private void switchView(String text) {
				if (text.endsWith(" Wszystko")){
					clientFactory.getPlaceController().goTo(new IncomeOutcomePlace());
				}else if (text.endsWith(" Wydatki")){
					clientFactory.getPlaceController().goTo(new OutcomePlace());
				}else if (text.endsWith(" Przychody")){
					clientFactory.getPlaceController().goTo(new IncomePlace());
				}

			}
		});

		mailPanelRoot.setState(true);
		return mailPanel;
	}

	private Widget createChartsItem(UiImages uiImages) {
		Tree mailPanel = new Tree();
		TreeItem mailPanelRoot = mailPanel.addItem(this.clientFactory.getUserData().getGoogleNickname());
		addItem(mailPanelRoot, uiImages.charts(),"Zestawienia");
		mailPanel.addSelectionHandler(new SelectionHandler()
		{
			@Override
			public void onSelection(SelectionEvent event)
			{
				switchView(((TreeItem) event.getSelectedItem()).getText());
			}

			private void switchView(String text) {
				if (text.endsWith("Zestawienia")){
					clientFactory.getPlaceController().goTo(new ChartPlace());
				}

			}
		});

		mailPanelRoot.setState(true);
		return mailPanel;
	}


	private Widget createLoanItem(UiImages uiImages) {
		Tree mailPanel = new Tree();
		TreeItem mailPanelRoot = mailPanel.addItem(this.clientFactory.getUserData().getGoogleNickname());
		addItem(mailPanelRoot, uiImages.loans()," Kredyty");
		mailPanel.addSelectionHandler(new SelectionHandler()
		{
			@Override
			public void onSelection(SelectionEvent event)
			{
				switchView(((TreeItem) event.getSelectedItem()).getText());
			}

			private void switchView(String text) {
				if (text.endsWith("Kredyty")){
					clientFactory.getPlaceController().goTo(new LoanPlace());
				}

			}
		});

		mailPanelRoot.setState(true);
		return mailPanel;
	}	  

	private Widget createInvestmentsItem(UiImages uiImages) {
		Tree mailPanel = new Tree();
		TreeItem mailPanelRoot = mailPanel.addItem(this.clientFactory.getUserData().getGoogleNickname());
		addItem(mailPanelRoot, uiImages.incomesAndOutcomes(),"Lokaty");
		addItem(mailPanelRoot, uiImages.investments(), "Giełda" );
		mailPanel.addSelectionHandler(new SelectionHandler()
		{
			@Override
			public void onSelection(SelectionEvent event)
			{
				switchView(((TreeItem) event.getSelectedItem()).getText());
			}

			private void switchView(String text) {
				if (text.endsWith("Lokaty")){
					clientFactory.getPlaceController().goTo(new InvestmentPlace());
				}else if (text.endsWith("Giełda")){
					clientFactory.getPlaceController().goTo(new StockPlace());
				}

			}
		});
		mailPanelRoot.setState(true);
		return mailPanel;
	}	

	private Widget createPlanerItem(UiImages uiImages) {
		Tree mailPanel = new Tree();
		TreeItem mailPanelRoot = mailPanel.addItem(this.clientFactory.getUserData().getGoogleNickname());
		addItem(mailPanelRoot, uiImages.planer(),"Planer");
		mailPanel.addSelectionHandler(new SelectionHandler()
		{
			@Override
			public void onSelection(SelectionEvent event)
			{
				switchView(((TreeItem) event.getSelectedItem()).getText());
			}

			private void switchView(String text) {
				if (text.endsWith("Planer")){
					clientFactory.getPlaceController().goTo(new SavingsPlace());

				}

			}
		});
		mailPanelRoot.setState(true);
		return mailPanel;
	}
	private Widget createCategoryItem(UiImages uiImages) {
		Tree mailPanel = new Tree();
		TreeItem mailPanelRoot = mailPanel.addItem(this.clientFactory.getUserData().getGoogleNickname());
		addItem(mailPanelRoot, uiImages.categories(),"Kategorie");
		mailPanel.addSelectionHandler(new SelectionHandler()
		{
			@Override
			public void onSelection(SelectionEvent event)
			{
				switchView(((TreeItem) event.getSelectedItem()).getText());
			}

			private void switchView(String text) {
				if (text.endsWith("Kategorie")){
					clientFactory.getPlaceController().goTo(new CategoryPlace());
				}

			}
		});
		mailPanelRoot.setState(true);
		return mailPanel;
	}	
	private Widget createImportItem(UiImages uiImages) {
		Tree mailPanel = new Tree();
		TreeItem mailPanelRoot = mailPanel.addItem(this.clientFactory.getUserData().getGoogleNickname());
		addItem(mailPanelRoot, uiImages.importOutcomes(),"Importuj");
		mailPanel.addSelectionHandler(new SelectionHandler()
		{
			@Override
			public void onSelection(SelectionEvent event)
			{
				switchView(((TreeItem) event.getSelectedItem()).getText());
			}

			private void switchView(String text) {
				if (text.endsWith("Importuj")){
					clientFactory.getPlaceController().goTo(new BankStatementImportPlace());
				}

			}
		});
		mailPanelRoot.setState(true);
		return mailPanel;
	}	


	private Widget createInfoItem(UiImages uiImages) {
		Tree mailPanel = new Tree();
		TreeItem mailPanelRoot = mailPanel.addItem(this.clientFactory.getUserData().getGoogleNickname());
		addItem(mailPanelRoot, uiImages.help(),"Kontakt");
		mailPanel.addSelectionHandler(new SelectionHandler()
		{
			@Override
			public void onSelection(SelectionEvent event)
			{
				switchView(((TreeItem) event.getSelectedItem()).getText());
			}

			private void switchView(String text) {
				if (text.endsWith("Kontakt")){
					clientFactory.getPlaceController().goTo(new OutcomePlace());
				}

			}
		});
		mailPanelRoot.setState(true);
		return mailPanel;
	}	
	private TreeItem addItem(TreeItem root, ImageResource image, String label) {
		SafeHtmlBuilder sb = new SafeHtmlBuilder();
		if (image!=null){
			sb.append(SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(
					image).getHTML()));
		}
		sb.appendEscaped(" ").appendEscaped(label);
		return root.addItem(sb.toSafeHtml());
	}


}
